<?php
/**
 * 管理后台 Dashboard 数据组装
 *
 * Author: Zeon
 * Date: 2018/5/4
 * Time: 11:10
 * Created by PanxSoft.
 */
namespace App\Services\Assemblers\Admin;


use App\Models\Panx\Sex;
use Carbon\Carbon;
use Illuminate\Support\Collection;

class DashboardAssembler
{
    /**
     * 微信用户画像
     *
     * @param  $wechat_users
     * @return array
     */
    public static function assembleWxUserProfiles(Collection $wechat_users)
    {
        return [
            'sex_ratio' => [
                [
                    'sex' => Sex::UNKNOWN,
                    'count' => $wechat_users->where('sex', Sex::UNKNOWN)->count()
                ],
                [
                    'sex' => Sex::MALE,
                    'count' => $wechat_users->where('sex', Sex::MALE)->count()
                ],
                [
                    'sex' => Sex::FEMALE,
                    'count' => $wechat_users->where('sex', Sex::FEMALE)->count()
                ],
            ],

            'born_range' => [
                [
                    'range' => trans('service.range_70'),
                    'count' => $wechat_users
                        ->where('born_at', "<", new Carbon('1980-01-01 00:00:00'))
                        ->count()
                ],
                [
                    'range' => trans('service.range_80'),
                    'count' => $wechat_users
                        ->where('born_at', ">=", new Carbon('1980-01-01 00:00:00'))
                        ->where('born_at', "<", new Carbon('1990-01-01 00:00:00'))
                        ->count()
                ],
                [
                    'range' => trans('service.range_90'),
                    'count' => $wechat_users
                        ->where('born_at', ">=", new Carbon('1990-01-01 00:00:00'))
                        // !!! 这里一定要写成 2000-01-01 00:00:00 不能写 2000，
                        // 不知道为什么 1999 是对的， 2000 就会变成 2018 年
                        ->where('born_at', "<", new Carbon('2000-01-01 00:00:00'))
                        ->count()
                ],
                [
                    'range' => trans('service.range_00'),
                    'count' => $wechat_users
                        ->where('born_at', ">=", new Carbon('2000-01-01 00:00:00'))
                        ->where('born_at', "<", new Carbon('2010-01-01 00:00:00'))
                        ->count()
                ],
                [
                    'range' => trans('service.range_10'),
                    'count' => $wechat_users
                        ->where('born_at', ">=", new Carbon('2010-01-01 00:00:00'))
                        ->where('born_at', "<", new Carbon('2030-01-01 00:00:00'))
                        ->count()
                ],
            ],

            'city_area' => $wechat_users
                ->groupBy('city')
                ->map(function (Collection $city_group, string $city) {
                    return [
                        'city' => $city ?: trans('service.tip'),
                        'count' => $city_group->count(),
                    ];
                })
                ->values()
        ];
    }
}